package com.ys.malini.dao.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ys.malini.dao.HibernatePagerManager;
import com.ys.malini.dao.ICompanyDao;
import com.ys.malini.model.TCompanyinfo;
import com.ys.malini.web.form.CompanyInfoForm;

public class CompanyDao extends HibernateDaoSupport implements ICompanyDao{

	public CompanyDao(){
		
	}
	
	/**
	 * 企业信息查询
	 * 
	 */
	public List<TCompanyinfo> query(CompanyInfoForm form, boolean isPager) {
		String hsql = " from TCompanyinfo order by  compId desc";
		List<TCompanyinfo> companylist = null;
		if (isPager) {
			companylist = HibernatePagerManager.searchPaginated(getSession(),
					hsql.toString());
		} else {
			companylist = getHibernateTemplate().find(hsql);
		}

		return companylist;
	}

	/**
	 * 单个企业信息查看
	 * 
	 */
	public TCompanyinfo queryCompanyInfoById(String id) {
		String hsql=" from TCompanyinfo where compId='"+id+"'";
		
		TCompanyinfo companyinfo = (TCompanyinfo) getHibernateTemplate().find(hsql).get(0);
		return companyinfo;
	}

	/**
	 * 企业信息更新
	 * @param id
	 */
	public String updateCompanyInfoById(CompanyInfoForm formbean){
		String id = formbean.getCompId().toString();
		String hsql=" from TCompanyinfo where compId='"+id+"'";
		TCompanyinfo companyinfo = (TCompanyinfo) getHibernateTemplate().find(hsql).get(0);
		String message="更新失败";
		try {
			companyinfo.setCompType(formbean.getCompType());
			companyinfo.setContent(formbean.getContent());
			getSession().merge(companyinfo);
			message="更新成功";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	
		return message;
	}

	
	/**
	 * 
	 * 新增单个公司信息
	 */
	public String addCompanyInfo(CompanyInfoForm formbean){
		String message="新增失败";

		TCompanyinfo companyinfo = new TCompanyinfo();
		try {
			companyinfo.setCompType(formbean.getCompType());
			companyinfo.setContent(formbean.getContent());
			getSession().save(companyinfo);
			message="新增成功";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
		
		return message;
	}

	
	/**
	 * 
	 * 删除单个企业信息
	 */
	public String deleteCompanyInfo(Map map){
		String message="删除失败";
		String compId=(String) map.get("compId");
		String compType= (String) map.get("compType");
		StringBuffer hql = new StringBuffer(" delete TCompanyinfo o where 1=1  and compId='"+compId+"' and compType='"+compType+"'");
		
		Query query = getSession().createQuery(hql.toString());		
		query.executeUpdate();
		message="删除成功！";
		return message;
	}

	//企业信息查询
	
	public TCompanyinfo queryTop(String compType){
		String hsql=" from TCompanyinfo where compType='"+compType+"' order by compId desc";
		
		Query query = getSession().createQuery(hsql);
		query.setFirstResult(0);
		query.setMaxResults(1);
		List list =query.list();
		TCompanyinfo companyinfo =list!=null&&list.size()>0?(TCompanyinfo)list.get(0):null;

		return companyinfo;
	}


}
